package com.lysj.bhp.wallet.service;

import com.lysj.bhp.wallet.base.WalletContent;
import com.lysj.bhp.wallet.domain.DiamondBitcoinWallet;
import com.lysj.bhp.wallet.repository.DiamondBitcoinWalletRepository;
import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.NetworkParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class DiamonBitcoinWalletService extends WalletContent {
    private static Logger logger = LoggerFactory.getLogger(DiamonBitcoinWalletService.class);
    private static NetworkParameters params = NetworkParameters.fromPmtProtocolID("main");

    public DiamondBitcoinWallet createDiamondBitcoinWallet(Long userId) {
        ECKey key = new ECKey();
        DiamondBitcoinWallet diamondBitcoinWallet = new DiamondBitcoinWallet();
        diamondBitcoinWallet.setUserId(userId);
        diamondBitcoinWallet.setAddress(key.toAddress(params).toString());
        diamondBitcoinWallet.setPrivKey(key.getPrivateKeyEncoded(params).toString());

        diamondBitcoinWallet.setBalance(BigDecimal.ZERO);
        diamondBitcoinWallet.setFreezeBalance(BigDecimal.ZERO);
        diamondBitcoinWallet.setAvailBalance(BigDecimal.ZERO);
        diamondBitcoinWallet.setBookKeepBalance(BigDecimal.ZERO);
        //------------增加钱包的抵押冻结资产----------------
        diamondBitcoinWallet.setPledgeFreezeBalance(BigDecimal.ZERO);
        logger.info("创建了一个新的钱包:" + diamondBitcoinWallet);
        diamondBitcoinWalletRepository.save(diamondBitcoinWallet);
        return diamondBitcoinWallet;
    }
}
